﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Windows.Media.Animation;
using System.Diagnostics;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for CreateDeployWindow.xaml
    /// </summary>
    public partial class CreateDeployWindow : Window
    {
        String ServiceName, DeploymentSlot;
        ServiceController.ServiceController serviceController;
        private Storyboard startbusy;
        private Storyboard stopbusy;
        public CreateDeployWindow(String _ServiceName, String _DeploymentSlot)
        {
            InitializeComponent();
            ServiceName = _ServiceName;
            DeploymentSlot = _DeploymentSlot;
            TextBoxDeployName.Focus();
            serviceController = new ServiceController.ServiceController();
            serviceController.CreateComplete += new ServiceController.ServiceControllerEventHandler(serviceController_CreateComplete);
            startbusy = (Storyboard)this.FindResource("StartBusy");
            stopbusy = (Storyboard)this.FindResource("StopBusy");
        }

        void serviceController_CreateComplete(object sender, ServiceController.ServiceControllerEventArgs e)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() =>
            {
                HideMask();
                if (e.error != null)
                {
                    MessageBox.Show(e.error.Message,"Error",MessageBoxButton.OK,MessageBoxImage.Error);
                }
                else
                {

                    MessageBox.Show("Create deploy operation completed successfully.","Information",MessageBoxButton.OK,MessageBoxImage.Information);
                    this.DialogResult = true;
                    this.Close();

                }
            }));


        }

        private void BtnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog OpenFiledialog = new OpenFileDialog();
            OpenFiledialog.Title = "Select a configuration file";
            OpenFiledialog.Filter = "Configuration files (*.cscfg)|*.cscfg|All files (*.*)|*.*";

            if ((bool)OpenFiledialog.ShowDialog())
            {
                TextBoxConfigFileLocation.Text = OpenFiledialog.FileName.ToString();
            }
        }

        private void BtnDeploy_Click(object sender, RoutedEventArgs e)
        {

            if (checkValidation())
            {


                ShowMask();
                serviceController.CreateDeployAsync(App.SubscriptionID, App.CertificatePath, ServiceName, DeploymentSlot,TextBoxDeployName.Text, TextBoxPkgUrl.Text, TextBoxConfigFileLocation.Text, TextBoxLabel.Text,null);





            }

        }

        private bool checkValidation()
        {
            if (String.IsNullOrEmpty(TextBoxDeployName.Text))
            {
                MessageBox.Show("Deploy name cannot be empty.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                TextBoxDeployName.Focus();
                return false;
            }
            if (String.IsNullOrEmpty(TextBoxPkgUrl.Text))
            {
                MessageBox.Show("Application Package Url cannot be empty.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                TextBoxPkgUrl.Focus();
                return false;
            }


            if (String.IsNullOrEmpty(TextBoxConfigFileLocation.Text))
            {
                MessageBox.Show("Configuration file Location cannot be empty.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                TextBoxConfigFileLocation.Focus();
                return false;
            }

            if (String.IsNullOrEmpty(TextBoxLabel.Text))
            {
                MessageBox.Show("Label cannot be empty.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                TextBoxLabel.Focus();
                return false;
            }

            return true;
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void HideMask()
        {
            startbusy.Stop();
            stopbusy.Begin();
            border2_Copy.Visibility = Visibility.Collapsed;
        }

        private void ShowMask()
        {
            border2_Copy.Visibility = Visibility.Visible;
            startbusy.Begin();
        }

        private void HandleRequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            string navigateUri = hl.NavigateUri.ToString();
            Process.Start(new ProcessStartInfo(navigateUri));
            e.Handled = true; 
        }
        
    }
}
